package web.day2.mapper;


import org.apache.ibatis.annotations.*;
import web.day2.pojo.User;

import java.util.List;

public interface UserMapper {
    /**
     * 按多个条件分页查询用户信息列表
     * @param startNo   页码
     * @param pageSize  页大小
     * @param loginName 登录名
     * @param realName  真实姓名
     * @return          返回用户实体对象集合
     */
    @Select("<script>SELECT * from user "
            + "WHERE 1=1 "
            + "<if test='loginName != null and loginName.trim().length() >0'>"
            + " AND loginName=#{loginName}"
            + "</if>"
            + "<if test='realName != null and realName.trim().length() >0'>"
            + " AND userName like CONCAT('%',#{realName},'%')"
            + "</if>"
            + " ORDER BY id ASC "
            + " LIMIT #{startNo},#{pageSize} "
            + "</script>")
    List<User> getUsersByPaging(
            @Param("startNo") int startNo,
            @Param("pageSize") int pageSize,
            @Param("loginName") String loginName,
            @Param("realName") String realName);

    /**
     * 按多个条件查询用户总条数
     * @param loginName
     * @param realName
     * @return
     */
    @Select("<script>SELECT count(0) from user "
            + "WHERE 1=1 "
            + "<if test='loginName != null and loginName.trim().length() >0'>"
            + " AND loginName=#{loginName}"
            + "</if>"
            + "<if test='realName != null and realName.length() >0'>"
            + " AND userName like CONCAT('%',#{realName},'%')"
            + "</if>"
            + "</script>")
    long getUserTotalCount(
            @Param("loginName") String loginName,
            @Param("realName") String realName);

    /**
     * 根据用户的Id查询用户信息
     * @param id
     * @return
     */
    @Select("SELECT * from `user` " +
            "where id = #{id}")
    User getUser(int id);

    /**
     * 通过用户Id列表获得用户信息列表
     * @param userId
     * @return
     */
    @Select("<script>"+"SELECT * from `user`" +
            "where 1=1 " +
            "AND id in " +
            "<foreach collection='list' open='(' separator=',' close=')' item='id'>" +
            "#{id}" +
            "</foreach>"+"</script>")
    List<User> getUserListByIds(List<Integer> userId);

    /**
     * 根据用户Id修改指定用户的信息
     * @param user  用户实体对象
     * @return
     */
    @Update({"update `user` set loginName = #{loginName}," +
            "userName = #{userName}, " +
            "password = #{password}, " +
            "sex = #{sex}, " +
            "identityCode = #{identityCode}, " +
            "email = #{email}, " +
            "mobile = #{mobile}, " +
            "type = #{type} " +
            "where id = #{id}"})
    int updateUser(User user);

    /**
     * 根据用户id删除指定用户信息
     * @param userId
     * @return
     */
    @Delete({"delete from `user` where id=#{id}"})
    int deleteUser(int userId);

    /**
     * 添加用户信息
     * @param user
     * @return
     */
    @Insert({ "insert into `user`(loginName, userName, password, sex,identityCode,email,mobile,type) values " +
            "(#{loginName}, #{userName}, #{password}, #{sex}, #{identityCode},#{email},#{mobile},#{type})" })
    int insertUser(User user);

    /**
     * 批量更新用户信息
     * @param userList
     * @return
     */
    @Update({
            "<script>",
            "<foreach collection='list' item='item' index='index' separator=';'>",
            "update  `user` ",
            "set loginName= #{item.loginName} , userName= #{item.userName} ," ,
            " password=#{item.password} , sex=#{item.sex} ,identityCode=#{item.identityCode} ,email=#{item.email} ," ,
            "mobile=#{item.mobile} ,type=#{item.type} where id= #{item.id} ",
            "</foreach>",
            "</script>"
    })
    int updateBatch(List<User> userList);

    /**
     * 批量逻辑删除用户信息
     * @param userList
     * @return
     */
    @Delete( {"<script>",

            "DELETE FROM  `user` WHERE id in",
            "<foreach collection='list' item='item' open='('  close=')' separator=','>" ,
            "#{item.id}",
            "</foreach>",
            "</script>"})
    int deleteBatch(List<User> userList);

    /**
     * 批量插入用户信息
     * @param userList
     * @return
     */
    @Insert({ "<script>",
            "insert into `user`(loginName, userName, password, sex,identityCode,email,mobile,type) values ",
            "<foreach collection='list' item='item' index='index' separator=','>",
            "( #{item.loginName},#{item.userName},#{item.password},#{item.sex},#{item.identityCode},#{item.email},#{item.mobile},#{item.type}) ",
            "</foreach>", "</script>" })
    int insertBatch(List<User> userList);
}